<?php

    class PhpDbDriverOci extends PhpDbAbstract // Driver ORACLE (pdo_oci est experimental)
    {
        public $conn = NULL;
        public $transaction = false;

        # constructeur
        public function __construct ( $dsn, $username = NULL, $password = NULL, $driver_options = array() )
        {
            parent::__construct($dsn,$username,$password,$driver_options);

            $cs = '';
            if(!empty($this->dsn['host']))
                $cs .= '//'.$this->dsn['host'];
            if(!empty($this->dsn['port']))
                $cs .= ':'.$this->dsn['port'];
            if(!empty($this->dsn['dbname']))
                $cs .= (empty($cs)?'':'/').$this->dsn['dbname']; // service
            $fct = 'oci_connect';
            if(!empty($this->dsn['persistent']))
                $fct = 'oci_pconnect';
            if(!empty($this->dsn['charset']))
                $this->conn = $fct($username,$password,$cs,$this->dsn['charset']);
            else
                $this->conn = $fct($username,$password,$cs); // ici le charset est lu dans  $NLS_LANG
            if(!$this->conn)
            {
                $e = oci_error();
                throw new Exception(htmlentities($e['message']));
            }

            // TODO setAttribute
            $this->setAttribute(PDO::ATTR_DRIVER_NAME,'oci');
        }

        # methodes PhpDb
        /*bool*/ public static function isAvailable()
        {
            return function_exists('oci_connect');
        }

        # methodes PDO
        /*bool*/ public function beginTransaction ( )
        {
            return $this->transaction = true;
        }
        /*bool*/ public function commit ( )
        {
            $this->transaction = false;
            return oci_commit($this->conn);
        }
        /*mixed*/ public function errorCode ( )
        {
            throw new Exception('NOT IMPLEMENTED : '.__FILE__.'@'.__LINE__.';'.__FUNCTION__);
        }
        /*array*/ public function errorInfo ( )
        {
            return oci_error($this->conn);
        }
        /*int*/ public function exec ( $statement )
        {
            // TODO utiliser le PhpDbStatement ?
            $statement = oci_parse($this->conn, $statement);
            if(!$statement)
            {
                $e = oci_error($this->conn);
                throw new Exception(htmlentities($e['message']));
            }
            $result = oci_execute($statement);
            if(!$result)
            {
                $e = oci_error($statement);
                throw new Exception(htmlentities($e['message']));
            }
            return $result;
        }
        /*mixed*/ public function getAttribute ( $attribute )
        {
            if(isset($this->attributes[$attribute]))
                return $this->attributes[$attribute];
            return NULL;
        }
        /*string*/ public function lastInsertId ( $name = NULL )
        {
            throw new Exception('NOT IMPLEMENTED : '.__FILE__.'@'.__LINE__.';'.__FUNCTION__);
        }
        /*PDOStatement*/ public function prepare ( $statement, $driver_options = array() )
        {
            $statement = new PhpDbStatement($this,$statement);
            foreach($driver_options as $name=>$value)
                $statement->setAttribute($name,$value);
            return $statement;
        }
        /*PDOStatement*/ public function query ( $statement )
        {
            $statement = new PhpDbStatement($this,$statement);
            $statement->execute();
            return $statement;
        }
        /*string*/ public function quote ( $string, $parameter_type = self::PARAM_STR )
        {
            throw new Exception('NOT IMPLEMENTED : '.__FILE__.'@'.__LINE__.';'.__FUNCTION__);
        }
        /*bool*/ public function rollBack ( )
        {
            $this->transaction = false;
            return oci_rollBack($this->conn);
        }
        /*bool*/ public function setAttribute ( $attribute, $value )
        {
            $this->attributes[$attribute] = $value; // TODO en faire quelque chose
        }
    }

?>
